package com.ecoolex.discount.sales.api.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.ecoolex.discount.sales.api.core.ApiConstant;
import com.ecoolex.discount.sales.ws.UnionPaySignatureService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Entry;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.device.DeviceInfo;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.user.system.entity.UserLoginRecord;
import com.ecoolex.user.system.entity.UserRoot;
import com.ecoolex.user.system.enums.UserLoginSource;
import com.ecoolex.user.system.enums.UserOperLogType;
import com.ecoolex.user.system.enums.UserRootSource;
import com.ecoolex.user.system.pojo.LoginUserRoot;
import com.ecoolex.user.system.ws.UserSystemService;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "第三方平台用户跳转登录")
public class ThirdPartyLoginGraphql {

	@Reference
	private UnionPaySignatureService unionPaySignatureService;
	
	@Reference
    private UserSystemService userSystemService;
	
	@QueryField(field = "unionPayUserLogin", description = "银联云闪付用户跳转登录")
	public LoginUserRoot login(@NonNull JSONObject request_data,DeviceInfo deviceInfo, ConsumerMemberAuthority user) {
		//验签
		Assert.isTrue(unionPaySignatureService.checkSignature(request_data),ResultCode.VERIFY.build(1), "验签失败");
		//获取用户信息
		JSONObject certificateSignature = request_data.getJSONObject("trxInfo");
		String mobile = certificateSignature.getString("mobile");
		//解密手机号等信息
		String mobileEncrypt = unionPaySignatureService.decrypt(mobile);
		String[] mobileArray = mobileEncrypt.split("-");
		String countryCode = mobileArray[0];
		String phone = mobileArray[1];
		//存在则登录
		UserRoot root;
		if(userSystemService.existByPhone(countryCode, phone)) {
			root = userSystemService.getByPhone(countryCode, phone);
			// 登录
	        UserLoginRecord record = userSystemService.login(root.getUserId(), UserLoginSource.THIRD_PARTY_API, "UnionPay", deviceInfo, ApiConstant.PROJECT_API_NAME);
	        // 刷新token
	        user.refresh(record.getToken());
		}else {
			root = userSystemService.authorizationRegister(UserRootSource.THIRD_PARTY,"UnionPay",countryCode,phone,null,null,ApiConstant.PROJECT_API_NAME);
			// 登录
	        UserLoginRecord record = userSystemService.login(root.getUserId(), UserLoginSource.THIRD_PARTY_API, "UnionPay", deviceInfo, ApiConstant.PROJECT_API_NAME);
	        // 刷新token
	        user.refresh(record.getToken());
	        // 操作日志
	        user.operationLog(UserOperLogType.REGISTER.real(), "银联云闪付自动注册", Entry.create("user", root));
		}
		LoginUserRoot loginUserRoot = new LoginUserRoot();
        loginUserRoot.setLoginSource(UserLoginSource.THIRD_PARTY_API);
        return BeanUtil.copy(root, loginUserRoot);
	}
}
